<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
      <el-form-item label="任务号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入盘点任务号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="计划编号" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择盘点状态">
            <el-option v-for="dict in dict.type.storehouse_check_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="盘点方式" prop="checkWay">
        <el-select v-model="queryParams.checkWay" placeholder="请选择盘点方式">
            <el-option v-for="dict in dict.type.storehouse_check_way" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="盘点类型" prop="checkType">
          <el-select v-model="queryParams.checkType" placeholder="请选择盘点方式">
              <el-option v-for="dict in dict.type.storehouse_check_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否复盘" prop="isRepeatCheck">
        <el-input
          v-model="queryParams.isRepeatCheck"
          placeholder="请输入是否复盘"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="盘点员" prop="checker">
        <el-input
          v-model="queryParams.checker"
          placeholder="请输入盘点员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="盘点员" prop="checkerName">
        <el-input
          v-model="queryParams.checkerName"
          placeholder="请输入盘点员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['storehouse:stockCheck:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['storehouse:stockCheck:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['storehouse:stockCheck:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-view"
          size="mini"
          @click="handleDetail"
          :disabled="single"
          v-hasPermi="['storehouse:stockCheck:detail']"
        >详情</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          size="mini"-->
<!--          icon="el-icon-c-scale-to-original"-->
<!--          @click="handleDiff"-->
<!--          :disabled="single"-->
<!--          v-hasPermi="['storehouse:stockCheck:diff']"-->
<!--        >差异</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAssign"
          :disabled="!(!single && selection[0].status == 'CREATE')"
          v-hasPermi="['storehouse:stockCheck:assign']"
        >分配</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="startCheck"
          :disabled="!canStart"
          v-hasPermi="['storehouse:stockCheck:start']"
        >开始盘点</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-refresh-left"
          size="mini"
          :disabled="!canRecheck"
          @click="handleRecheck"
          v-hasPermi="['storehouse:stockCheck:recheck']"
        >复盘</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="cancelCheck"
          :disabled="!canCancel"
          v-hasPermi="['storehouse:stockCheck:cancel']"
        >取消盘点</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-view"
          size="mini"
          @click="handleDifference"
          :disabled="single"
          v-hasPermi="['storehouse:stockCheck:difference']"
        >差异对比</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-view"
          size="mini"
          @click="handleAuditDifference"
          :disabled="single"
          v-hasPermi="['storehouse:stockCheck:auditDifference']"
        >差异审核</el-button>
      </el-col>
          <!-- :disabled="!(!single && selection[0].status == 'COMPLETE')" -->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-check"-->
<!--          size="mini"-->
<!--          :disabled="!(!single && selection[0].status == 'COMPLETE')"-->
<!--          @click="handleFinish"-->
<!--          v-hasPermi="['storehouse:stockCheck:finish']"-->
<!--        >结束盘点</el-button>-->
<!--      </el-col>-->

      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['storehouse:stockCheck:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockCheckList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <af-table-column label="盘点任务号" align="center" prop="no" />
      <af-table-column label="盘点状态" align="center" prop="status" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.storehouse_check_status" :value="scope.row.status" />
        </template>
      </af-table-column>
      <!-- <el-table-column label="计划编号" align="center" prop="planNo" /> -->
      <af-table-column label="盘点类型" align="center" prop="checkType">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.storehouse_check_type" :value="scope.row.checkType" />
        </template>
      </af-table-column>
      <af-table-column label="盘点方式" align="center" prop="checkWay">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.storehouse_check_way" :value="scope.row.checkWay" />
        </template>
      </af-table-column>
      <af-table-column label="盘点仓库" align="center" prop="storehouseNo" />
      <af-table-column label="盘点计划" align="center" prop="planName" />
      <!-- <el-table-column label="盘点员" align="center" prop="checker" /> -->
      <el-table-column label="盘点员名称" align="center" prop="checkerName" min-width="180"/>
      <af-table-column label="是否复盘" alisys_yes_nogn="center" prop="isRepeatCheck">
        <template slot-scope="scope" >
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isRepeatCheck"  />
        </template>
      </af-table-column>
      <el-table-column label="完成时间" alisys_yes_nogn="center"  prop="completeTime"  width="152" />
      <!--<el-table-column label="关闭时间" alisys_yes_nogn="center"  prop="closeTime"  width="152" />-->
      <el-table-column :label="$t('createUser')" align="center" prop="createBy" min-width="100"/>
      <el-table-column :label="$t('createTime')" align="center" prop="createTime" width="152" />
      <el-table-column :label="$t('updateUser')" align="center" prop="updateBy" min-width="100"/>
      <el-table-column :label="$t('updateTime')" align="center" prop="updateTime" width="152" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['storehouse:stockCheck:detail']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDiff(scope.row)"
            v-hasPermi="['storehouse:stockCheck:diff']"
          >差异</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['storehouse:stockCheck:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存盘点任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="no" prop="no">
          <el-input v-model="form.no" placeholder="请输入no" />
        </el-form-item>
        <el-form-item label="计划编号" prop="planNo">
          <el-input v-model="form.planNo" placeholder="请输入计划编号" />
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称" />
        </el-form-item>
        <el-form-item label="盘点方式:PRODUCT-物料;LOCATION-位置" prop="checkWay">
          <el-input v-model="form.checkWay" placeholder="请输入盘点方式:PRODUCT-物料;LOCATION-位置" />
        </el-form-item>
        <el-form-item label="是否复盘" prop="isRepeatCheck">
          <el-input v-model="form.isRepeatCheck" placeholder="请输入是否复盘" />
        </el-form-item>
        <el-form-item label="盘点员" prop="checker">
          <el-input v-model="form.checker" placeholder="请输入盘点员" />
        </el-form-item>
        <el-form-item label="盘点员名称" prop="checkerName">
          <el-input v-model="form.checkerName" placeholder="请输入盘点员名称" />
        </el-form-item>
        <el-form-item label="是否删除：1是 0否" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除：1是 0否" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分配库存盘点任务对话框 -->
    <el-dialog title="分配盘点任务" :visible.sync="openAllocation" width="500px" append-to-body>
      <el-form ref="allocationForm" :model="form" :rules="allocationRules" label-width="80px">
        <el-form-item label="人员" prop="checker">
          <el-select style="width: 100%" v-model="checkerList" multiple filterable placeholder="请选择人员">
            <el-option v-for="dict in userList" :key="dict.userName" :label="dict.nickName" :value="dict.userName"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAllocationForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 差异详情对话框 -->
    <el-dialog title="盘点差异" :visible.sync="openDiffInfo" width="70%" append-to-body>
      <el-table  :data="diffList" >
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="盘点时间" align="center" prop="createTime" />
        <el-table-column label="仓库" align="center" prop="storehouseNo" />
        <el-table-column label="库区" align="center" prop="areaNo" />
        <el-table-column label="库位" align="center" prop="locationNo" />
        <el-table-column label="物料编号" align="center" prop="productNo" />
        <el-table-column label="物料名称" align="center" prop="productName" />
        <el-table-column label="条码" align="center" prop="barcode" />
        <el-table-column label="库存数量" align="center" prop="stockQty" />
        <el-table-column label="盘点数量" align="center" prop="checkQty" />
        <el-table-column label="单位" align="center" prop="productUnit" />
        <el-table-column label="盘点差异" align="center" prop="diffQty" />
      </el-table>


    </el-dialog>

    <!-- 差异详情对话框 -->
    <el-dialog title="盘点范围" :visible.sync="openDetailInfo" width="70%" append-to-body>

      <el-form ref="detailForm" :model="detailForm"  label-width="80px" :inline="true">

        <el-form-item label="单据号" prop="no">
          <el-input disabled v-model="detailForm.no" />
        </el-form-item>
        <el-form-item label="状态" prop="checkWay">
          <el-select disabled v-model="detailForm.status">
            <el-option v-for="dict in dict.type.storehouse_check_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点类型">
          <el-select disabled v-model="detailForm.checkType">
            <el-option v-for="dict in dict.type.storehouse_check_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点方式" prop="checkWay">
          <el-select disabled v-model="detailForm.checkWay" placeholder="请选择盘点方式">
            <el-option v-for="dict in dict.type.storehouse_check_way" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点仓库" prop="storehouseNo">
          <el-input disabled v-model="detailForm.storehouseNo" />
        </el-form-item>
      </el-form>

      <el-table v-if="detailForm.checkWay == 'PRODUCT'"  :data="detailList" >
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="物料编号" align="center" prop="productNo" />
        <el-table-column label="物料名称" align="center" prop="productName" />
      </el-table>

      <el-table v-if="detailForm.checkWay == 'LOCATION'"    :data="detailList" >
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="仓库" align="center" prop="storehouseNo" />
        <el-table-column label="库区" align="center" prop="areaNo" />
        <el-table-column label="库位" align="center" prop="locationNo" />
      </el-table>


    </el-dialog>

    <!-- 差异对比对话框 -->
    <el-dialog title="差异对比" :visible.sync="openDiffInfo" width="70%" append-to-body>

      <el-form ref="detailForm" :model="detailForm"  label-width="80px" :inline="true">

        <div>
        <el-form-item >
            <el-button type="primary" @click="submitReview">提交差异申请</el-button>
            <el-button @click="cancel">关闭</el-button>
        </el-form-item>
        </div>
        <el-form-item label="单据号" prop="no">
          <el-input disabled v-model="detailForm.no" />
        </el-form-item>
        <el-form-item label="状态" prop="checkWay">
          <el-select disabled v-model="detailForm.status">
            <el-option v-for="dict in dict.type.storehouse_check_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点类型">
          <el-select disabled v-model="detailForm.checkType">
            <el-option v-for="dict in dict.type.storehouse_check_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点方式" prop="checkWay">
          <el-select disabled v-model="detailForm.checkWay" placeholder="请选择盘点方式">
            <el-option v-for="dict in dict.type.storehouse_check_way" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点仓库" prop="storehouseNo">
          <el-input disabled v-model="detailForm.storehouseNo" />
        </el-form-item>
      </el-form>

      <div style="font-size: 16px;margin-top: 10px;margin-bottom: 10px">盘点范围</div>

      <el-table :data="detailList" v-if="detailForm.checkWay == 'PRODUCT'">
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="物料编号" align="center" prop="productNo" />
        <el-table-column label="物料名称" align="center" prop="productName" />
      </el-table>

      <el-table :data="detailList" v-if="detailForm.checkWay == 'LOCATION'">
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="仓库" align="center" prop="storehouseNo" />
        <el-table-column label="库区" align="center" prop="areaNo" />
        <el-table-column label="库位" align="center" prop="locationNo" />
      </el-table>

      <el-tabs style="margin-top: 20px" v-model="activeTab" type="card" >
        <el-tab-pane label="差异明细" name="detail">
          <el-table :data="differenceList" row-key="id">
            <el-table-column label="序号" type="index" width="50" />
            <el-table-column label="库位" align="center" prop="locationNo" />
            <el-table-column label="库区" align="center" prop="areaNo" />
            <el-table-column label="仓库" align="center" prop="storehouseNo" />
            <el-table-column label="条码" align="center" prop="barcode" />
            <el-table-column label="物料编号" align="center" prop="productNo" />
            <el-table-column label="物料名称" align="center" prop="productName" />
            <el-table-column label="库存数量" align="center" prop="stockQty" />
            <el-table-column label="盘点数量" align="center" prop="checkQty" />
            <el-table-column prop="diffQty" align="center" label="差异数量">
              <template slot-scope="scope">
                <span :style="{'color': scope.row.diffQty != 0 ? 'red' : 'inherit'}">{{ scope.row.diffQty }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="diffDesc" align="center" label="差异说明">
              <template slot-scope="scope">
                 <el-input v-model="scope.row.diffDesc" @change="changeDiffDesc(scope.row)"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="盘点时间" align="center" prop="createTime" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="所有盘点明细" name="allDetail">
          <el-table :data="allDifferenceList">
            <el-table-column label="序号" type="index" width="50" />
            <el-table-column label="库位" align="center" prop="locationNo" />
            <el-table-column label="库区" align="center" prop="areaNo" />
            <el-table-column label="仓库" align="center" prop="storehouseNo" />
            <el-table-column label="条码" align="center" prop="barcode" />
            <el-table-column label="物料编号" align="center" prop="productNo" />
            <el-table-column label="物料名称" align="center" prop="productName" />
            <el-table-column label="库存数量" align="center" prop="stockQty" />
            <el-table-column label="盘点数量" align="center" prop="checkQty" />
            <el-table-column prop="diffQty" align="center" label="差异数量">
              <template slot-scope="scope">
                <span :style="{'color': scope.row.diffQty != 0 ? 'red' : 'inherit'}">{{ scope.row.diffQty }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="diffDesc" align="center" label="差异说明">
              <template slot-scope="scope">
                <el-input v-model="scope.row.diffDesc" @change="changeDiffDesc(scope.row)"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="盘点时间" align="center" prop="createTime" />
          </el-table>
        </el-tab-pane>
      </el-tabs>

    </el-dialog>

    <!-- 差异审核对话框 -->
    <el-dialog title="差异审核" :visible.sync="openAuditDiffInfo" width="70%" append-to-body>

      <el-form ref="detailForm" :model="detailForm"  label-width="80px" :inline="true">

        <div>
        <el-form-item >
            <el-button type="primary" @click="reviewPass">通过差异申请</el-button>
            <el-button type="primary" @click="reviewFail">拒绝差异申请</el-button>
        </el-form-item>
        </div>
        <el-form-item label="单据号" prop="no">
          <el-input disabled v-model="detailForm.no" />
        </el-form-item>
        <el-form-item label="状态" prop="checkWay">
          <el-select disabled v-model="detailForm.status">
            <el-option v-for="dict in dict.type.storehouse_check_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点类型">
          <el-select disabled v-model="detailForm.checkType">
            <el-option v-for="dict in dict.type.storehouse_check_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点方式" prop="checkWay">
          <el-select disabled v-model="detailForm.checkWay" placeholder="请选择盘点方式">
            <el-option v-for="dict in dict.type.storehouse_check_way" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点仓库" prop="storehouseNo">
          <el-input disabled v-model="detailForm.storehouseNo" />
        </el-form-item>
      </el-form>

      <div style="font-size: 16px;margin-top: 10px;margin-bottom: 10px">盘点范围</div>

      <el-table :data="detailList" v-if="detailForm.checkWay == 'PRODUCT'">
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="物料编号" align="center" prop="productNo" />
        <el-table-column label="物料名称" align="center" prop="productName" />
      </el-table>

      <el-table :data="detailList" v-if="detailForm.checkWay == 'LOCATION'">
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="仓库" align="center" prop="storehouseNo" />
        <el-table-column label="库区" align="center" prop="areaNo" />
        <el-table-column label="库位" align="center" prop="locationNo" />
      </el-table>

      <el-form style="margin-top: 30px" ref="detailForm" :model="detailForm"  label-width="80px" :inline="true">
        <el-form-item label="审核意见">
          <el-input style="width: 300px;" v-model="detailForm.auditSug" />
        </el-form-item>
      </el-form>

      <el-tabs style="margin-top: 20px" v-model="activeTab" type="card" >
        <el-tab-pane label="差异明细" name="detail">
          <el-table :data="differenceList">
            <el-table-column label="序号" type="index" width="50" />
            <el-table-column label="库位" align="center" prop="locationNo" />
            <el-table-column label="库区" align="center" prop="areaNo" />
            <el-table-column label="仓库" align="center" prop="storehouseNo" />
            <el-table-column label="条码" align="center" prop="barcode" />
            <el-table-column label="物料编号" align="center" prop="productNo" />
            <el-table-column label="物料名称" align="center" prop="productName" />
            <el-table-column label="库存数量" align="center" prop="stockQty" />
            <el-table-column label="盘点数量" align="center" prop="checkQty" />
            <el-table-column prop="diffQty" align="center" label="差异数量">
              <template slot-scope="scope">
                <span :style="{'color': scope.row.diffQty != 0 ? 'red' : 'inherit'}">{{ scope.row.diffQty }}</span>
              </template>
            </el-table-column>
            <el-table-column label="差异说明" align="center" prop="diffDesc" />
            <el-table-column label="盘点时间" align="center" prop="createTime" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="所有盘点明细" name="allDetail">
          <el-table :data="allDifferenceList">
            <el-table-column label="序号" type="index" width="50" />
            <el-table-column label="库位" align="center" prop="locationNo" />
            <el-table-column label="库区" align="center" prop="areaNo" />
            <el-table-column label="仓库" align="center" prop="storehouseNo" />
            <el-table-column label="条码" align="center" prop="barcode" />
            <el-table-column label="物料编号" align="center" prop="productNo" />
            <el-table-column label="物料名称" align="center" prop="productName" />
            <el-table-column label="库存数量" align="center" prop="stockQty" />
            <el-table-column label="盘点数量" align="center" prop="checkQty" />
            <el-table-column prop="diffQty" align="center" label="差异数量">
              <template slot-scope="scope">
                <span :style="{'color': scope.row.diffQty != 0 ? 'red' : 'inherit'}">{{ scope.row.diffQty }}</span>
              </template>
            </el-table-column>
            <el-table-column label="差异说明" align="center" prop="diffDesc" />
            <el-table-column label="盘点时间" align="center" prop="createTime" />
          </el-table>
        </el-tab-pane>
      </el-tabs>

    </el-dialog>
  </div>
</template>

<script>
import { listStockCheck,
  listCheckDiff,
  listCheckDetail,
  getStockCheck,
  delStockCheck,
  startCheck,
  cancelCheck,
  submitReview,
  addStockCheck,
  updateStockCheck,
  allocation,
  createReCheck,
  finishStockCheck,
  listDifferenceDetail,
  createReCheckDirect,
  reviewPass,
  reviewFail,
  changeDiffDesc
} from "@/api/storehouse/stockCheck";
import { listUser } from "@/api/system/user";

export default {
  name: "StockCheck",
  dicts: ["sys_yes_no","storehouse_check_type","storehouse_check_way","storehouse_check_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selection: [],
      differenceList: [],
      allDifferenceList: [],
      activeTab:'detail',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存盘点任务表格数据
      stockCheckList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        planNo: null,
        planName: null,
        checkType: null,
        checkWay: null,
        status: null,
        isRepeatCheck: null,
        checker: null,
        checkerName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      allocationRules: {
        checkerList: [
          { required: true, message: "人员不能为空", trigger: "blur" }
        ]
      },
      openAllocation: false,
      openDiffInfo: false,
      openAuditDiffInfo: false,
      openDetailInfo: false,
      userList:[],
      checkerList:[],
      detailList:[],
      diffList:[],
      detailForm:{},
      canRecheck:false,
      canStart:false,
      canCancel:false,
    };
  },
  created() {
    this.getList();
    this.getListUser()
  },
  methods: {
      handleAssign(row) {
        this.reset();
        console.log(row.id)
        this.form = { id: row.id }
        if(this.form.checker){
            this.checkerList = this.form.checker.split(",")
        }else{
            this.checkerList = []
        }
        this.openAllocation = true
      },
    /** 查询库存盘点任务列表 */
    getList() {
      this.loading = true;
      listStockCheck(this.queryParams).then(response => {
        this.stockCheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取物料列表 */
    getListUser() {
      listUser({ pageNum: 1, pageSize: 0 }).then((response) => {
        this.userList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openAllocation = false;
      this.openDiffInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        planNo: null,
        planName: null,
        checkType: null,
        checkWay: null,
        status: "0",
        isRepeatCheck: null,
        checker: null,
        checkerName: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null,
        delFlag: null,
        checkerList:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.selection = selection
      this.single = selection.length!==1
      this.multiple = !selection.length
      // 判断勾选数据是否全部都是盘点中,是则允许开始盘点
      this.canStart = true;
      this.canCancel = true;
      if (this.selection.length == 0){
        this.canStart = false;
        this.canCancel = false;
      }
      if (this.selection.length == 1 && this.selection[0].status != 'CANCEL'){
        this.canRecheck = true;
      }else {
        this.canRecheck = false;
      }
      selection.forEach(item => {
        if (item.status != 'ALLOCATION') {
          this.canStart = false;
        }
      });
      selection.forEach(item => {
        if (item.status != 'CREATE' && item.status != 'ALLOCATION' && item.status != 'WAIT') {
          this.canCancel = false;
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存盘点任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStockCheck(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存盘点任务";
      });
    },
    submitAllocationForm(){
      if(!this.checkerList.length){
        this.$modal.msgError("请选择人员");
        return
      }
      this.$refs["allocationForm"].validate(valid => {
        if (valid) {
            let checkerName = []
            this.checkerList.forEach(item => {
                let checker = this.userList.find(subItem => subItem.userName == item)
                if(checker){
                    checkerName.push(checker.nickName)
                }
            });
            allocation({id:this.ids[0],checker:this.checkerList.join(','),checkerName: checkerName.join(',')}).then(res=>{
              this.$modal.msgSuccess("分配成功");
              this.openAllocation = false;
              this.getList();
            })
        //   if (this.form.id != null) {
        //     updateStockCheck(this.form).then(response => {
        //       this.$modal.msgSuccess("修改成功");
        //       this.open = false;
        //       this.getList();
        //     });
        //   } else {
        //     addStockCheck(this.form).then(response => {
        //       this.$modal.msgSuccess("新增成功");
        //       this.open = false;
        //       this.getList();
        //     });
        //   }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStockCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStockCheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('deleteTip', { num: ids.length })).then(function() {
        return delStockCheck(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 开始盘点按钮操作 */
    startCheck(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认开始盘点').then(function() {
        return startCheck(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 取消盘点按钮操作 */
    cancelCheck(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认取消盘点').then(function() {
        return cancelCheck(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 复盘按钮操作 */
    handleRecheck(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否复盘?').then(function() {
        return createReCheck(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 结束盘点按钮操作 */
    handleFinish(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确定结束盘点?').then(()=> {
        this.$confirm('是否复盘?','系统提示',{
            distinguishCancelAndClose: true,
            confirmButtonText: '是',
            cancelButtonText: '否'
          }).then(()=> {
          createReCheckDirect(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("操作成功");
          })
        }).catch(action=>{
          if(action=== 'cancel'){
            finishStockCheck(ids).then(()=>{
              this.getList();
              this.$modal.msgSuccess("操作成功");
            })
          }
        });
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storehouse/stockCheck/export', {
        ...this.queryParams
      }, `stockCheck_${new Date().getTime()}.xlsx`)
    },
    // 打开差异详情页面
    handleDiff(){
      // 根据任务号获取差异详情
      listCheckDiff({
        page:1,
        size:99999,
        checkNo: this.selection[0].no
      }).then(res => {
        this.diffList = res.rows;
        this.diffList.forEach((detailRow) => {
          if (detailRow.checkQty && detailRow.stockQty) {
            detailRow.diffQty = parseInt(detailRow.checkQty) - parseInt(detailRow.stockQty);
          }
        });
        this.openDiffInfo = true;
      });
    },
    // 打开详情页面
    handleDetail(row){
      this.checkWay = this.selection[0].checkWay;
      this.detailForm = this.selection[0];
      // 根据任务号获取详情
      listCheckDetail({
        page:1,
        size:99999,
        checkNo: this.selection[0].no
      }).then(res => {
        this.detailList = res.rows;
        this.openDetailInfo = true;
      });
    },
    // 打开差异页面
    handleDifference(){
      this.checkWay = this.selection[0].checkWay;
      this.detailForm = this.selection[0];
      this.openDiffInfo = true;
      // 根据任务号获取详情
      listCheckDetail({
        page:1,
        size:99999,
        checkNo: this.selection[0].no
      }).then(res => {
        this.detailList = res.rows;
      });
      listDifferenceDetail({
        checkNo: this.selection[0].no,
        isDiff:'1'
      }).then(res => {
        res.rows.forEach((detailRow) => {
          detailRow.diffQty = parseInt(detailRow.checkQty) - parseInt(detailRow.stockQty);
          if(!detailRow.diffDesc) detailRow.diffDesc = '';
        });
        this.differenceList = res.rows;
      });
      listDifferenceDetail({
        checkNo: this.selection[0].no,
      }).then(res => {
        this.allDifferenceList = res.rows;
        this.allDifferenceList.forEach((detailRow) => {
          detailRow.diffQty = parseInt(detailRow.checkQty) - parseInt(detailRow.stockQty);
        });
      });
    },
    // 打开差异审核页面
    handleAuditDifference(){
      this.checkWay = this.selection[0].checkWay;
      this.detailForm = this.selection[0];
      this.openAuditDiffInfo = true;
      // 根据任务号获取详情
      listCheckDetail({
        page:1,
        size:99999,
        checkNo: this.selection[0].no
      }).then(res => {
        this.detailList = res.rows;
      });
      listDifferenceDetail({
        checkNo: this.selection[0].no,
        isDiff:'1'
      }).then(res => {
        this.differenceList = res.rows;
        this.differenceList.forEach((detailRow) => {
          detailRow.diffQty = parseInt(detailRow.checkQty) - parseInt(detailRow.stockQty);
        });
      });
      listDifferenceDetail({
        checkNo: this.selection[0].no,
      }).then(res => {
        this.allDifferenceList = res.rows;
        this.allDifferenceList.forEach((detailRow) => {
          detailRow.diffQty = parseInt(detailRow.checkQty) - parseInt(detailRow.stockQty);
        });
      });
    },
    /** 提交差异申请 */
    submitReview() {
      let id = this.detailForm.id;
      this.$modal.confirm('是否确认提交').then(function() {
        return submitReview(id);
      }).then(() => {
        this.getList();
        this.openDiffInfo = false;
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 通过差异申请 */
    reviewPass() {
      let obj = {
        id:this.detailForm.id,
        auditSug:this.detailForm.auditSug
      }
      this.$modal.confirm('是否确认通过').then(function() {
        return reviewPass(obj);
      }).then(() => {
        this.getList();
        this.openAuditDiffInfo = false;
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 拒绝差异申请 */
    reviewFail() {
      let obj = {
        id:this.detailForm.id,
        auditSug:this.detailForm.auditSug
      }
      this.$modal.confirm('是否确认拒绝').then(function() {
        return reviewFail(obj);
      }).then(() => {
        this.getList();
        this.openAuditDiffInfo = false;
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    changeDiffDesc(row){
      return changeDiffDesc(row);
    }
  }
};
</script>
